package public

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/public"
	publicRes "gitcode.com/G-YT/ANN/server/model/public/response"
)

type OnlineServiceService struct {
}

// FindQuestionByWhere 通过条件获取单条问题信息
// Author: [g-qs](https://github.com/g-qs)
func (onlineServiceService *OnlineServiceService) FindQuestionByWhere(condition map[string]interface{}, fields string) (Info publicRes.QuestionResult, err error) {
	var questionInfo public.OnlineServiceQuestion
	if condition == nil {
		condition = map[string]interface{}{"pid": 0}
	}
	err = global.GVA_DB.Order("q_id Desc").Limit(1).Select(fields).Where(condition).Find(&questionInfo).Error
	Info = publicRes.QuestionResult{
		Qid:      questionInfo.ID,
		Pid:      questionInfo.Pid,
		Question: questionInfo.Question,
		Type:     questionInfo.Types,
		Info:     questionInfo.Info,
	}
	IsLastStatus := inIntArray(questionInfo.ID, []uint{7, 10, 11, 12, 16, 17, 19, 22, 25, 26})
	if IsLastStatus == true {
		Info.IsLast = 1
	} else {
		Info.IsLast = 2
	}
	return Info, err
}

// SubmitQuestionnaire 通过条件获取单条问题信息
// Author: [g-qs](https://github.com/g-qs)
func (onlineServiceService *OnlineServiceService) SubmitQuestionnaire(answer *public.OnlineServiceAnswer) (err error) {
	err = global.GVA_DB.Create(&answer).Error
	if err != nil {
		return err
	}
	return err
}

// 检查数值是否存在于切片中
func inIntArray(val uint, arr []uint) bool {
	for _, v := range arr {
		if val == v {
			return true
		}
	}
	return false
}
